#include <iostream>
// #include "util.hpp"
// #include "level.hpp"
// #include "format.hpp"
// #include "sink.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"
#include "../logs/log.h"

void test_log(const std::string& log_name)
{
    Logger::ptr logger = getLogger(log_name);
    std::cout << "---------测试开始-------------" << std::endl;
    logger->debug("测试日志");
    logger->info("测试日志");
    logger->warn("测试日志");
    logger->error("测试日志");
    logger->fatal("测试日志");
    std::cout << "---------测试结束-------------" << std::endl;
}

int main()
{
    // 功能测试
    std::unique_ptr<LoggerBuilder> builder(new GlobalLoggerBuilder());
    builder->buildLoggerName("sync-logger");
    builder->buildLoggerLevel(LogLevel::value::UNKNOW);
    builder->buildFormatter("[%c]%m%n");
    builder->buildSink<StdoutSink>();
    builder->buildLoggerType(LoggerType::LOGGER_SYNC);
    builder->buildSink<FileSink>("./logfile/sync.log"); 
    builder->buildSink<RollBySizeSink>("./logfile/roll-sync-by-size-", 1024*1024);
    builder->build();

    test_log("sync-logger");
    // std::cout << date::now() << std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // std::cout << file::exists(pathname) << std::endl;
    // std::cout << file::path(pathname) << std::endl;
    // file::create_directory(file::path(pathname));

    // std::cout << LogLevel::toString(LogLevel::value::DEBUG) << std::endl;
    // std::cout << LogLevel::toString(LogLevel::value::INFO) << std::endl;
    // std::cout << LogLevel::toString(LogLevel::value::WARN) << std::endl;
    // std::cout << LogLevel::toString(LogLevel::value::OFF) << std::endl;

    // LogMsg msg(LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    // Formatter fmt;
    // std::string str = fmt.format(msg);
    // //std::cout << str << std::endl;

    // LogSink::ptr logsink = SinkFactory::create<StdoutSink>();
    // //LogSink::ptr logsink2 = SinkFactory::create<FileSink>("./logfile/test.log");
    // //LogSink::ptr logsink3 = SinkFactory::create<RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // //logsink->log(str.c_str(), str.size());
    // //logsink2->log(str.c_str(), str.size());
    // LogSink::ptr logsink4 = SinkFactory::create<RollByTimeSink>("./logfile/roll-", TimeGap::GAP_SECOND);

    // // size_t cursize = 0;
    // // size_t count = 0;
    // time_t old = Date::now();
    // while (Date::now() < old + 10)
    // {
    //     //std::string tmp = str + std::to_string(count++);
    //     logsink4->log(str.c_str(), str.size());
    //     //cursize += str.size();
    // }

    // std::string logger_name = "sync_logger";
    // LogLevel::value limit = LogLevel::value::WARN;
    // Formatter::ptr fmt(new Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // //LogSink::ptr stdout_lsp = SinkFactory::create<StdoutSink>();
    // //LogSink::ptr file_lsp = SinkFactory::create<FileSink>("./logfile/test.log");
    // LogSink::ptr roll_lsp = SinkFactory::create<RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // std::vector<LogSink::ptr> sinks = {roll_lsp};
    // Logger::ptr logger(new SyncLogger(logger_name, limit, fmt, sinks));

    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    // std::string str = "测试日志";
    // size_t cursize = 0;
    // size_t count = 0;
    // time_t old = Date::now();
    // while (cursize < 1024 * 1024 * 5)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    //     cursize += 65;
    // }

    // size_t cursize = 0;
    // size_t count = 0;
    // time_t old = Date::now();

    // std::string str = "测试日志";
    // size_t cursize = 0;
    // size_t count = 0;
    // time_t old = Date::now();
    // while (cursize < 1024 * 1024 * 5)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    //     cursize += 65;
    // }
    // // 读取数据，一点点写入缓冲区，把缓冲区写入到文件，判断md5是否一致
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false)
    // {
    //     return -1;
    // }

    // // 获取文件大小
    // ifs.seekg(0, std::ios::end);
    // size_t fsize = ifs.tellg();  // 读写位置偏移量
    // ifs.seekg(0, std::ios::beg);
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);  // 传string，转const char*无法修改内容，所以饶了一下，
    // if (ifs.good() == false) return -1;
    // ifs.close();

    // Buffer buff;
    // for (int i = 0; i < body.size(); i++)
    // {
    //     buff.push(&body[i], 1);
    // }

    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // ofs.write(buff.begin(), buff.readableSize());
    // 一点点写入
    // int size = 0;
    // for (int i = 0; size < buff.readableSize(); i++)  // 可读大小一直在变小
    // {
    //     ofs.write(buff.begin(), 1);
    //     buff.moveReader(1);
    // }
    
    return 0;
}